//
//  WBNewFeatureView.swift
//  swiftweibo
//
//  Created by ffxk007 on 2017/6/22.
//  Copyright © 2017年 ffxk007. All rights reserved.
//

import UIKit

/// 新特性视图
class WBNewFeatureView: UIView {

    @IBOutlet weak var scrollView: UIScrollView!
    @IBOutlet weak var entreButton: UIButton!
    
    @IBOutlet weak var controlPage: UIPageControl!
    //进入微博
    @IBAction func enterStatus() {
        
        removeFromSuperview()
    }
    
    class func newFeatureView()-> WBNewFeatureView {
        let nib = UINib(nibName: "WBNewFeatureView", bundle: nil)
        let v = nib.instantiate(withOwner: nil, options: nil)[0] as! WBNewFeatureView
        
        v.frame = UIScreen.main.bounds
        
        return v
        
    }
    
    
    override func awakeFromNib() {
        //如果使用自动布局设置的界面，从xib加载默认的是600*600大小
        //添加四个图像视图
        let count = 3
        let rect = UIScreen.main.bounds
        
        for i in 0..<count {
            let imageName = "guide page\(i+1)"
            let iv = UIImageView(image: UIImage(named: imageName))
            
            //设置大小
            iv.frame = rect.offsetBy(dx: CGFloat(i) * rect.width, dy: 0)
            
            scrollView.addSubview(iv)
        }
        
        //指定scrollview的属性
        scrollView.contentSize = CGSize(width: CGFloat(count + 1) * rect.width, height: rect.height)
        scrollView.bounces = false
        scrollView.isPagingEnabled = true
        scrollView.showsVerticalScrollIndicator = false
        scrollView.showsHorizontalScrollIndicator = false
        scrollView.delegate = self
        
        //隐藏按钮
        entreButton.isHidden = true
        
    }

}

extension WBNewFeatureView:UIScrollViewDelegate {
    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        //滚动到最后一屏，让视图删除
        let page = Int(scrollView.contentOffset.x / scrollView.bounds.width)
        
        //判断是否最后一页
        if page == scrollView.subviews.count {
            removeFromSuperview()
        }
        
        //如果是倒数第二页显示按钮
       entreButton.isHidden = ( page != scrollView.subviews.count - 1)
        
    }
    
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        //一旦滚动 隐藏按钮
        entreButton.isHidden = true
        
        //计算当前的偏移量
        let page = Int(scrollView.contentOffset.x / scrollView.bounds.width + 0.5)
        
        //设置分页控件
        controlPage.currentPage = page
        
        //分页控件的隐藏
        controlPage.isHidden = (page == scrollView.subviews.count)
    }
}
